import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import java.util.*;
import java.util.List;

public class SchoolUI extends JFrame implements ActionListener{

	private JLabel name,ssn,no,sub,emp; // 이름 주민 학번 과목 부서
	private JTextField tx_name, tx_ssn, tx_no,tx_sub,tx_emp; // 상동 //
	private JRadioButton st,tc,em; // 라디오 버튼 학생 교수 직원
	private JButton reset,btn_save,delet,update;
	private JList <Person> jlist;
	private JScrollPane scroll;
	private DefaultListModel<Person> model = new DefaultListModel<Person>(); 
	private SchoolManager sm = new SchoolManager();
	public static final int STUDENT = 1;
	public static final int TEACHER = 2;
	
	// model.addElements(); 추가 메소드  // model.removeElements();// 삭제 
	public SchoolUI(){
		super("School Manager");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		jlist = new JList<Person>();
		jlist.setModel(model);
		// 라벨
		name = new JLabel("이름");
		ssn = new JLabel("주민등록번호");
		no = new JLabel("학번");
		sub = new JLabel("과목");
		emp = new JLabel("부서");
		
		// 텍스트 필드
		tx_name = new JTextField(20);
		tx_ssn = new JTextField(15);
		tx_no = new JTextField(20);
		tx_sub = new JTextField(20);
		tx_emp = new JTextField(20);
		// 초기값 학생 나머지 비활성화
		tx_sub.setEnabled(false);
		tx_emp.setEnabled(false);

		//라디오 버튼
		st = new JRadioButton("학생",true);
		tc = new JRadioButton("교수");
		em = new JRadioButton("직원");
		
		ButtonGroup group = new ButtonGroup();
		
		group.add(st);
		group.add(tc);
		group.add(em);

		// 버튼 
		reset = new JButton("폼초기화");
		btn_save = new JButton("저장");
		btn_save.addActionListener(this);
		delet = new JButton("삭제");
		update = new JButton("수정");

		// 무명 클래스 리스너 등록
		// 폼 초기화

		// Jlist 
		scroll = new JScrollPane(jlist);
		scroll.setPreferredSize(new Dimension(200,100));
		
	}

	public void main(){
		JPanel south = new JPanel();
		JPanel north = new JPanel();
		JPanel west = new JPanel(new GridLayout(5,1));
		
		// west  속성  
		// 이름
		JPanel p1 = new JPanel();
		p1.add(name);
		p1.add(tx_name);
		// 주민 번호
		JPanel p2 = new JPanel();
		p2.add(ssn);
		p2.add(tx_ssn);
		//학번
		JPanel p3 = new JPanel();
		p3.add(no);
		p3.add(tx_no);
		//과목
		JPanel p4 = new JPanel();
		p4.add(sub);
		p4.add(tx_sub);
		//부서		
		JPanel p5 = new JPanel();
		p5.add(emp);
		p5.add(tx_emp);
		// west 담기
		west.add(p1);west.add(p2);west.add(p3);west.add(p4);west.add(p5);
		// north 에 라디오 추가 
		north.add(st);north.add(tc);north.add(em);
		// south 에 버튼 추가
		south.add(reset);south.add(btn_save);south.add(delet);south.add(update);

		//jframe 에 화면 구성

		add(west,BorderLayout.WEST);
		add(north,BorderLayout.NORTH);
		add(south,BorderLayout.SOUTH);
		add(scroll,BorderLayout.EAST);

		pack();
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getSource() == btn_save){
			String name = tx_name.getText();
			String ssn = tx_ssn.getText();
			if(st.isSelected()){
				String hakbun = tx_no.getText();
				
				if(name.equals("") || ssn.equals("") || hakbun.equals("")){
					JOptionPane.showMessageDialog(this, "필드를 입력하세요", "필드를 입력하세요", 1);
					return;
				}
				
				Person p = new Student(name, ssn, hakbun);
				try {
					sm.addPerson(p);
				} catch (DuplicatedException e) {
					JOptionPane.showMessageDialog(this, e.getMessage(), e.getMessage(), 1);
				}
				
				modelSync(STUDENT);
				
			}else if(tc.isSelected()){
				String sub =tx_sub.getText();
				
				if(name.equals("") || ssn.equals("") || sub.equals("")){
					JOptionPane.showMessageDialog(this, "필드를 입력하세요", "필드를 입력하세요", 1);
					return;
				}
				
				Person p = new Teacher(name, ssn, sub);
				try {
					sm.addPerson(p);
				} catch (DuplicatedException e) {
					JOptionPane.showMessageDialog(this, e.getMessage(), e.getMessage(), 1);
				}
				
				modelSync(TEACHER);
			}
		}
	}

	private void modelSync(int pos) {
		model.clear();
		List<Person> personList = sm.getPersonList();
		for (int i = 0; i < personList.size(); i++) {
			if(personList.get(i) instanceof Student && pos == STUDENT)
				model.addElement(personList.get(i));
			else if(personList.get(i) instanceof Student && pos == TEACHER){
				model.addElement(personList.get(i));
			}
		}
		
	}

	
}